package com.ocom.payconfig.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.ocom.common.entity.card.EntityCardRecord;
import com.ocom.payconfig.vo.GetCardRecordListVo;
import com.ocom.payconfig.vo.GetListRechargeRVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

@Mapper
public interface IDaoCardRecord extends BaseMapper<EntityCardRecord> {


    @Select({"<script>",
               "select cr.id,cr.com_id,cr.account_no,cr.p_id,cr.acc_type_id,pi.p_code,pi.p_name,\n" +
                       "cr.card_no,cr.operation_type,cr.cost_fee,cr.deposit,cr.create_user,cr.create_time FROM card_record cr \n" +
                       "LEFT JOIN personnel_info pi ON cr.com_id=pi.com_id and cr.p_id=pi.p_id \n"+
                       "where cr.com_id=#{comId}"+
                       " <when test=\"startTime!=null and endTime!=null\"> "+
                       " AND cr.create_time &gt;= #{startTime} and cr.create_time &lt;= #{endTime}"+
                       " </when> "+
                       " <when test=\"createUser!=null and createUser!=''\"> "+
                       " AND cr.create_user like concat('%', #{createUser}, '%') "+
                       " </when> "+
                       " <when test=\"cardNo!=null and cardNo!=''\"> "+
                       " AND cr.card_no like concat('%', #{cardNo}, '%') "+
                       " </when> "+
                       " <when test=\"deptId!=null \" >"+
                       " AND pi.dept_id=#{deptId}"+
                       " </when> "+
                       " <when test=\"operationType!=null and operationType!=0\"> "+
                       " AND cr.operation_type =#{operationType} "+
                       " </when> "+
                       "order by cr.create_time desc",
            "</script>"})
    List<GetCardRecordListVo> cardOperDetailList(@Param("comId") Long comId, @Param("startTime")Date startTime,
                                                 @Param("endTime")Date endTime,@Param("createUser")String createUser,
                                                 @Param("cardNo")String cardNo,@Param("operationType")Integer operationType,
                                                 @Param("deptId") Long deptId
    );

    @Select("SELECT * FROM (SELECT " +
            " COUNT(operation_type = 1 OR NULL) AS issuingTimes, " +
            " COUNT(operation_type = 3 OR NULL) AS checkoutTImes, " +
            " COUNT(operation_type = 5 OR NULL) AS lossTimes, " +
            " COUNT(operation_type = 6 OR NULL) AS unwindTimes, " +
            " SUM(cost_fee) AS costFeeSum, " +
            " create_user, " +
            " DATE_FORMAT(cr.create_time, '%Y-%m-%d') AS dayTime " +
            " FROM " +
            " card_record cr " +
            "LEFT JOIN personnel_info pi ON cr.com_id = pi.com_id AND cr.p_id = pi.p_id " +
            "${ew.customSqlSegment} ) pr")
    List<EntityCardRecord>  countCardOptRecord(@Param(Constants.WRAPPER) Wrapper wrapper);
}
